package com.bidevalution.business.vote.callbackService.impl;

import com.alibaba.druid.util.StringUtils;
import com.bidevalution.business.exception.VoteCallbackException;
import com.bidevalution.business.service.BeEvalutionOutcomeService;
import com.bidevalution.business.service.BeSectionExpertInfoService;
import com.bidevalution.business.service.BeSectionStepJobService;
import com.bidevalution.business.service.BeSectionStepService;
import com.bidevalution.business.vote.callbackService.VoteBaseCallback;
import com.bidevalution.business.vote.vi.CallStepJobVi;
import com.bidevalution.dao.entity.BeExpertVote;
import com.bidevalution.dao.entity.BeExpertVoteCallBack;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.dao.entity.VoteParams;
import com.bidevalution.dao.mapper.BeExpertVoteMapper;
import com.bidevalution.service.layout.LayoutCode;
import com.qtp.core.util.GsonUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Log4j2
@Component
public class UpdateSorceCallback implements VoteBaseCallback {
    @Autowired
    private BeEvalutionOutcomeService beEvalutionOutcomeService;
    @Autowired
    private BeSectionStepService beSectionStepService;
    @Autowired
    private BeSectionExpertInfoService beSectionExpertInfoService;


    @Autowired
    private BeSectionStepJobService beSectionStepJobService;

    @Autowired
    private BeExpertVoteMapper beExpertVoteMapper;

    @Override
    public CallStepJobVi callback(List<BeExpertVoteCallBack> voteCallBackList)  throws VoteCallbackException {
        CallStepJobVi callStepJobVi = null;
        for(BeExpertVoteCallBack beExpertVoteCallBack : voteCallBackList) {
            String voteParamsStr = beExpertVoteCallBack.getVoteParams();
            if (!StringUtils.isEmpty(voteParamsStr)) {
                VoteParams voteParams = GsonUtil.parseJsonWithGson(voteParamsStr, VoteParams.class);
                voteParams.setResult(beExpertVoteCallBack.getVoteResule());

                String sectionId = voteParams.getSectionId();
//                String expertId = voteParams.getExpertId();
                String sectionStepId = voteParams.getSectionStepId();
                String submitCode = voteParams.getSubmitCode();
                String layoutCode = voteParams.getLayoutCode();
                // 去掉分
                voteParams.setResult(voteParams.getResult().replaceAll("分",""));
                // 更新值
                beEvalutionOutcomeService.updateResult(voteParams);

                // 专家ID
                List<BeSectionExpertInfo> beSectionExpertInfos = beSectionExpertInfoService.getExpertListBySectionId(sectionId);

                // 执行子存储过程
                for (BeSectionExpertInfo item: beSectionExpertInfos) {
                    String expertId = item.getSeqId();
                    beEvalutionOutcomeService.callProcedure("p_update_score_technology", sectionId, sectionStepId, submitCode, layoutCode, expertId);
                }

                // 汇总数据
//                for (BeSectionExpertInfo item: beSectionExpertInfos) {
//                    String expertId = item.getSeqId();
//                    beEvalutionOutcomeService.callProcedure("p_save_summary", sectionId, beExpertVoteCallBack.getSectionStepId(), submitCode, LayoutCode.Center.name(), expertId);
//                }

            }

            // 进行下一个
            //BeExpertVoteCallBack beExpertVoteCallBack = voteCallBackList.get(0);
            BeExpertVote beExpertVote = beExpertVoteMapper.selectByPrimaryKey(beExpertVoteCallBack.getSeqId());

            callStepJobVi = (CallStepJobVi) GsonUtil.parseJsonWithGson(beExpertVote.getStepParams(), CallStepJobVi.class);

        }

        return callStepJobVi;

    }

}
